<?php
namespace app\index\controller;

use think\Db;

class Article extends Base{
    public function index(){
        $art_id = input('art_id');
        $arts = Db::name('article')
                        -> alias('a')
                        -> field('a.*, c.catename')
                        -> join('cate c', 'c.id = a.cate_id')
                        -> find($art_id);
        //浏览量
        db('article')->where('id', $art_id)->setInc('click');

        $prev = Db::name('article') -> where('id', '<', $art_id) -> where('cate_id', '=', $arts['cate_id']) ->order('id DESC') -> limit(1) -> value('id');
        $next = Db::name('article') -> where('id', '>', $art_id) -> where('cate_id', '=', $arts['cate_id']) ->order('id ASC') -> limit(1) -> value('id');

        //相关文章
        $relaters = $this ->relate($arts['keywords']);


        $this -> assign('arts', $arts);
        $this -> assign('prev', $prev);
        $this -> assign('next', $next);

        $this -> assign('relaters', $relaters);

        return $this -> fetch('article');
    }

    //相关文章
    public function relate($keywords){
        $arr = explode(',', $keywords);

        static $ralateres = array();
        foreach($arr as $k =>$v){
            $map['keywords'] = array('like', '%'.$v.'%');
            $arters = Db::name('article') -> field('id, title, time') -> where($map) -> order('id DESC') -> limit(10) -> select();

            $ralateres = array_merge($ralateres, $arters);
            //去除重复数组
            $ralateres = arr_unique($ralateres);
        }
        return $ralateres;
    }

    //留言
    public function message(){

    }



}